module.exports = {
	data() {
		return {

		}
	},
	methods: {
		openDetail: function(item) { //打开详情页,参数item产品信息
			if (item.type==2) {
				wx.navigateTo({
					url: '/pages/vipShiping/detail?id=' + item.id
				})
			} else {
				wx.navigateTo({
					url: '/pages/product/detail/detail?id=' + item.id
				})
			}
		},
		goSearch: function() { //打开搜索页
			wx.navigateTo({
				url: ""
			})
		},
		disabledEvent: function() {
			return false;
		},
		Deduplication: function(arr1, arr2) { //合并去重
			return [...new Set(arr1.concat(arr2))]
		},
		differenceSet: function(arr1, ...arrs) { //差集
			var all = [];
			for (var i = 0; i < arrs.length; i++) {
				all = all.concat(arrs[i])
			};
			return arr1.filter(function(a) {
				return !all.includes(a)
			});
		},
		dateFormatter: function(date, str) { //时间格式化
			if (!date) return str;
			var year = new Date(date).getFullYear();
			var month = (new Date(date).getMonth() + 1) > 9 ? (new Date(date).getMonth() + 1) : ('0' + (
				new Date(date).getMonth() + 1));
			var day = new Date(date).getDate() > 9 ? new Date(date).getDate() : ('0' + new Date(date)
		.getDate());
			var hours = new Date(date).getHours() > 9 ? new Date(date).getHours() : ('0' + new Date(date)
				.getHours());
			var minutes = new Date(date).getMinutes() > 9 ? new Date(date).getMinutes() : ('0' + new Date(date)
				.getMinutes());
			var seconds = new Date(date).getMilliseconds() > 9 ? new Date(date).getMilliseconds() : ('0' +
				new Date(date).getMilliseconds());
			return str.replace('yyyy', year).replace('MM', month).replace('dd', day).replace('HH', hours)
				.replace('mm', minutes).replace('ss', seconds);
		},
		remainder: function(date) { //剩余时间
			var res = {
				days: 0,
				hours: 0,
				minutes: 0,
				seconds: 0
			};
			var now = new Date().getTime();
			var end = new Date(date).getTime();
			if (end <= now) return res;
			res.days = parseInt((end - now) / (24 * 60 * 60 * 1000));
			res.hours = parseInt((end - now - res.days * (24 * 60 * 60 * 1000)) / (60 * 60 * 1000));
			res.minutes = parseInt((end - now - res.days * (24 * 60 * 60 * 1000) - res.hours * (60 * 60 *
				1000)) / (60 * 1000));
			res.seconds = parseInt((end - now - res.days * (24 * 60 * 60 * 1000) - res.hours * (60 * 60 *
				1000) - res.minutes * (60 * 1000)) / 1000);
			return res;
		},
		replaceWith: function(str, startLen, endLen, str1) { //字符替换，str:需要替换的字符,startLen:起始长度,endLen:结尾长度,str1:替换字符
			var res = str;
			var arr = str.split('');
			if (startLen + endLen < str.length) {
				res = arr.splice(0, startLen).join('') + str1 + arr.splice(arr.length - endLen).join('')
			};
			return res;
		},
		openSinglePage: function(id) {
			wx.navigateTo({
				url: "/pages/single/single?id=" + id
			})
		},
		groupAndSortByMonth: function(dateArray) {
			// 使用 reduce 方法将日期按年份和月份分组
			const groupedByYearMonth = dateArray.reduce((result, obj) => {
				const date = new Date(obj.date);
				const year = date.getFullYear();
				const month = date.getMonth() + 1;
				if (!result[year]) {
					result[year] = {};
				}
				if (!result[year][month]) {
					result[year][month] = [];
				}
				result[year][month].push(obj);
				return result;
			}, {});
			// 对每个年份和月份的对象数组进行排序
			var res = [];
			let yearkeys = Object.keys(groupedByYearMonth).sort((a, b) => {
				return b - a
			});
			for (var i = 0; i < yearkeys.length; i++) {
				var tmp = {
					name: yearkeys[i],
					list: []
				};
				let monthkeys = Object.keys(groupedByYearMonth[yearkeys[i]]).sort((a, b) => {
					return b - a
				});
				for (var j = 0; j < monthkeys.length; j++) {
					tmp.list.push({
						name: monthkeys[j],
						list: groupedByYearMonth[yearkeys[i]][monthkeys[j]].sort((a, b) => {
							const dateA = new Date(a.date);
							const dateB = new Date(b.date);
							return dateB - dateA;
						})
					});
				};
				res.push(tmp);
			};
			return res;
		},
	}
}